<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class BaseItemTreeBase extends BaseItemBase {
	
	//constructor
	function BaseItemTreeBase() {
		parent::BaseItemBase();
	}
	
	//methods
	function getItemsList($filter = '', $check = 0, $limit = true, $include_subs = true, $level = 1) {
		$rows = array();
		
		if( $level >= 5 ) {
			return $rows;
		}
		
		$items = $this->getItems($filter, $check, $limit);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$items[$i]['level'] = $level;
			$rows[] = $items[$i];
			
			if( $include_subs == true ) {
				if( !is_array($filter) ) {
					$filter = array($filter);
				}
				$filter['parent'] = $items[$i]['id'];
				$rows_sub = $this->getItemsList($filter, $check, $limit, $include_subs, $level + 1);
				if( sizeof($rows_sub) > 0 ) {
					foreach($rows_sub as $key => $row_sub) {
						$rows[] = $row_sub;
					}
				} else {
					//
				}
			}
		}
		
		return $rows;
	}
	
	function getItemsTree($filter = '', $parent = 0, $check = 0, $limit = true, $simple = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$items_tree = $this->getItemsList($filter, $check, $limit, true, '1');
		for($i=0, $n=sizeof($items_tree); $i<$n; $i++) {
			$prefix = '';
			if( $simple == true ) {
				for($j=1; $j<$items_tree[$i]['level']; $j++) {
					$prefix .= '&nbsp;&nbsp;&nbsp;';
				}
			} else {
				for($j=1; $j<$items_tree[$i]['level']; $j++) {
					$prefix .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				$prefix .= '&nbsp;&nbsp;|&nbsp;--&nbsp;';
			}
			
			$items_tree[$i]['name_prefix'] = $prefix;
			$items_tree[$i]['text'] = $prefix . $items_tree[$i]['name'];
		}
		
		return $items_tree;
	}
	
	function getItemsTreeFormat($filter = '', $parent = 0, $check = 0, $limit = true, $simple = false) {
		$items_tree = $this->getItemsTree($filter, $parent, $check, $limit, $simple);
		
		$items_tree_format = array();
		if( is_array($items_tree) ) {
			for($i=0, $n=sizeof($items_tree); $i<$n; $i++) {
				if( $simple == true ) {
					$items_tree_format[$items_tree[$i]['id']] = $items_tree[$i]['name'];
				} else {
					$items_tree_format[$items_tree[$i]['id']] = $items_tree[$i]['text'];
				}
			}
		}
		
		return $items_tree_format;
	}
	
	function getItemsSubs($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$items = $this->getItemsList($filter, $check, $limit, true);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			if( $items[$i]['parent'] == $parent ) {
				$subs[] = $items[$i];
			}
		}
		
		return $subs;
	}
	
	function getItemsSubsFormat($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$items = $this->getItemsList($filter, $check, $limit, true);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			if( $items[$i]['parent'] == $parent ) {
				$subs[] = array(
								'id' => $items[$i]['id'],
								'name' => $items[$i]['name'],
								'link' => $items[$i]['link'],
								'parent' => $items[$i]['parent'],
								'level' => $items[$i]['level'],
										);
			}
		}
		
		return $subs;
	}
	
	function getItemsSubsIDs($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$items = $this->getItemsList($filter, $check, $limit, true);
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			if( $items[$i]['parent'] == $parent ) {
				$items_subs[] = $items[$i]['id'];
			}
		}
		
		return $items_subs;
	}
	
	function getItemsTopLevel($filter = '', $check = 0, $limit = true) {
		$items_top = $this->getItemsSubs(0, $filter, $check, $limit);
		
		return $items_top;
	}
	
	function getItemsTopLevelFormat($filter = '', $check = 0, $limit = true) {
		$items_top = $this->getItemsTopLevel($filter, $check, $limit);
		
		$items_top_format = array();
		if( is_array($items_top) ) {
			for($i=0, $n=sizeof($items_top); $i<$n; $i++) {
				$items_top_format[$items_top[$i]['id']] = $items_top[$i]['name'];
			}
		}
		
		return $items_top_format;
	}
	
	function getItemsTwoLevel($parent = 0, $filter = '', $check = 0, $limit = true) {
		$items_top = $this->getItemsTopLevel($filter, $check, $limit);
		for($i=0, $n=sizeof($items_top); $i<$n; $i++) {
			$items_top[$i]['subs'] = $this->getItemsSubs($items_top[$i]['id'], $filter, $check, $limit);
		}
		
		return $items_top;
	}
	
	function getItemsThreeLevel($parent = 0, $filter = '', $check = 0, $limit = true) {
		$items_top = $this->getItemsTopLevel($filter, $check, $limit);
		for($i=0, $n=sizeof($items_top); $i<$n; $i++) {
			$items_subs = $this->getItemsSubs($items_top[$i]['id'], $filter, $check, $limit);
			for($j=0, $m=sizeof($items_subs); $j<$m; $j++) {
				$items_subs[$j]['subs'] = $this->getItemsSubs($items_subs[$j]['id'], $filter, $check, $limit);
			}
			$items_top[$i]['subs'] = $items_subs;
		}
		
		return $items_top;
	}
	
	function getItemsFourLevel($parent = 0, $filter = '', $check = 0, $limit = true) {
		$items_top = $this->getItemsTopLevel($filter, $check, $limit);
		for($i=0, $n=sizeof($items_top); $i<$n; $i++) {
			$items_subs = $this->getItemsSubs($items_top[$i]['id'], $filter, $check, $limit);
			for($j=0, $m=sizeof($items_subs); $j<$m; $j++) {
				$items_subs_subs = $this->getItemsSubs($items_subs[$j]['id'], $filter, $check, $limit);
				for($k=0, $l=sizeof($items_subs_subs); $k<$l; $k++) {
					$items_subs_subs[$k]['subs'] = $this->getItemsSubs($items_subs_subs[$k]['id'], $filter, $check, $limit);
				}
				$items_subs[$j]['subs'] = $items_subs_subs;
			}
			$items_top[$i]['subs'] = $items_subs;
		}
		
		return $items_top;
	}
	
	//
	function getItemParent($id, $p = 0) {
		if( $p == 0 ) {
			$item = $this->getItem($id, 'id');
			$p = $item['parent'];
		}
		if( $p == 0 ) {
			return NULL;	
		}
		$parent = $this->getItem($p, 'id');
		
		return $parent;
	}
	
	function getItemParentID($id, $p = 0) {
		$parent = $this->getItemParent($id, $p);
		if( is_array($parent) ) {
			return $parent['id'];	
		}
		
		return 0;
	}
	
	function getItemRoot($id, $p = 0) {
		$root = array();
		$parent = $this->getItemParent($id, $p);
		if( is_array($parent) ) {
			$id = $parent['id'];
			$p = $parent['parent'];
		}
		if( $p == 0 ) {
			$root = $this->getItem($id, 'id');	
		} else {
			$root = $this->getItemRoot($id, $p);
		}
		
		return $root;
	}
	
	function getItemRootID($id, $p = 0) {
		$root = $this->getItemRoot($id, $p);
		
		return $root['id'];
	}
}
//
?>